"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--磁盘
Case Name   : 备升主时，备节点磁盘IOwait高
Create At   : 2021/07/13
@zou_jialiang0505328126
Description :
    1.备节点注入IOwait高
    2.停止主节点
    3.备升主
Expect      :
    1.成功
    2.成功
    3.成功
History     :
    Modified 2021/9/1 @wan005:适配最新CFE故障打印
    Modified 2021/9/13 @wan005:备节点注入iowait高时可能导致refreshconf在备节点出现异常
"""

import unittest
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger



class CPUOverloadAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case051:开始执行---------')
        self.pri_root_node = Node(node='PrimaryRoot')
        self.sta1_root_node = Node(node='Standby1Root')
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        self.log.info(cmd)
        msg = self.sta1_root_node.sh(cmd).result()
        self.log.info(msg)
        self.dir = msg.splitlines()[-1].split()[-1].strip()
        self.hostmsg = self.pri_root_node.sh('hostname;').result()
        self.log.info(f'hostname is {self.hostmsg}')

    def test_main(self):
        text = '----step1: 备节点磁盘IOwait高 expect:成功----'
        self.log.info(text)
        inject_command = f"rIO_iowait (type,dir,bs,count,time) " \
            f"values (r_iowait,{self.dir}, 10000,10000,1000)"
        result = self.com.cfe_inject(self.sta1_root_node, inject_command)
        self.log.info(result)
        result = self.com.cfe_query(self.sta1_root_node, "rIO_iowait")
        self.log.info(result)
        self.assertIn(self.constant.cfe_iowait_success_msg, result,
                      '执行失败:' + text)

        text = '----step2: 停止主节点 expect:成功----'
        self.log.info(text)
        result = self.pri_sh.stopDbInstance()
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG, result,
                      '执行失败:' + text)

        text = '----step2: 备升主 expect:成功----'
        self.log.info(text)
        result = self.sta1_sh.executeGsctl('failover',
                                          self.constant.FAILOVER_SUCCESS_MSG)
        self.assertTrue(result, '执行失败:' + text)
        refresh_result = self.sta1_sh.exec_refresh_conf()
        if not refresh_result:
            self.log.info("--------------主节点清理故障-------------")
            for i in range(3):
                cfe_result = self.com.cfe_clean(self.sta1_root_node,
                                                "rIO_iowait")
                self.log.info(cfe_result)
            refresh_result = self.sta1_sh.exec_refresh_conf()
        self.assertTrue(refresh_result, '执行失败:' + text)

    def tearDown(self):
        self.log.info("--------------备节点清理故障-------------")
        result = self.com.cfe_clean(self.sta1_root_node, "rIO_iowait")
        self.log.info(result)
        self.log.info("---------------还原集群------------")
        self.pri_sh.executeGsctl(
            'start', 'start', '-M standby')
        self.pri_sh.executeGsctl(
            'switchover', self.constant.SWITCHOVER_SUCCESS_MSG)
        self.pri_sh.exec_refresh_conf()
        self.log.info('-----Opengauss_Reliability_Hardware_Case051:执行完成-----')
